<?php
class Game_getGameConf {

	function getGameConf() {
		$readByConf = MooConfig::get('main.readByConf');
		
		if($readByConf == 1) {
			$gameInfo = MooConfig::get('game');
		} else {
			$fetchServerUrl = MooConfig::get('main.url.fetch_game');
			$gameInfo = MooUtil::curl_send($fetchServerUrl);
			$gameInfo = MooJson::decode($gameInfo, true);
			$fetchServerNumUrl = MooConfig::get('main.url.fetch_server');
		}
		
		$gameConfs  = array();
		if($gameInfo) {
			foreach ($gameInfo as $key => $val) {
				$platforms = explode(",", $val['platform']);
				$gameConfs[$val['game']]['platform'] = $platforms;
				if ($val['serverNum']) {
					foreach ($platforms as $k => $v) {
						$gameConfs[$val['game']]['serverNum'][$v] = $val['serverNum'][$v];
					}
				} else {
					$url = $fetchServerNumUrl . "?game=" . $val['game'];
					$serverNums = MooUtil::curl_send($url);
					if ($serverNums) {
						$serverNums = MooJson::decode($serverNums, true);
						$nums = count($serverNums);
					} else {
						$nums = 0;
					}
					foreach ($platforms as $k => $v) {
						$serverIds = array();
						$num = $this->getServerNums($v, $serverNums, $serverIds);
						$gameConfs[$val['game']]['serverNum'][$v] = $num;
						$gameConfs[$val['game']]['serverIds'][$v] = $serverIds;
					}
				}
			}
		}
		
		return 	$gameConfs;
	}
	
	// 获取服务器信息数
	private function getServerNums($v, $serverNums, &$serverIds) {
		$num = 0;
		if($serverNums) {
			$i = 0;
			foreach ($serverNums as $key => $val) {
				if ($val['platform'] == $v) {
					$num ++;
					$serverIds[$i] = $val['server'];
					$i++;
				}
			}
		} 
		return $num;
	}
}
